package com.youxue.teachin.user.userSystem;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import com.Navigator;
import com.library.rest.ApiServiceManager;
import com.library.uiframework.utils.ContextUtils;
import com.library.uiframework.utils.StringUtils;
import com.youxue.teachin.common.utils.PreContent;
import com.youxue.teachin.common.utils.PreferencesUtils;
import com.youxue.teachin.common.vo.UserVO;
import com.youxue.teachin.youmen.UMenUtils;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-05 17:06
 * @version: 9.1.0
 */
public class UserSystem {
    /**
     * 登录成功广播
     */
    public static  final String LOGIN_SUCCESS = "action_login_success";
    /**
     * 登录失败广播
     */
    public static  final String LOGIN_OUT_SUCCESS = "action_login_out_success";

    private static final UserSystem USER_SYSTEM = new UserSystem();
    private UserVO mUserVO;
    public static String tokenId = "";
    public static String token = "";

    private UserSystem() {

    }

    public void clearToken() {
        PreferencesUtils.saveString(PreContent.TOKEN, "");
        PreferencesUtils.saveString(PreContent.TOKENID, "");
        ApiServiceManager.clearToken();
        tokenId = "";
        token = "";
    }

    public void setToken(String tokens, String tokenIds) {
        token = tokens;
        tokenId = tokenIds;
        saveToken();
    }

    private void saveToken() {
        PreferencesUtils.saveString(PreContent.TOKEN, token);
        PreferencesUtils.saveString(PreContent.TOKENID, tokenId);
        ApiServiceManager.token = token;
        ApiServiceManager.tokenId = tokenId;
    }

    public void init() {
        token = PreferencesUtils.getString(PreContent.TOKEN, "");
        tokenId = PreferencesUtils.getString(PreContent.TOKENID, "");
        saveToken();
    }

    public static UserSystem getInstance() {
        return USER_SYSTEM;
    }

    public UserSystem updateUser(UserVO userVO) {
        mUserVO = userVO;
        return this;
    }

    public UserVO getUserVO() {
        return mUserVO;
    }

    public boolean isLogin() {
        return StringUtils.isNotEmpty(token) && StringUtils.isNotEmpty(tokenId);
    }

    /**
     * 退出登录
     */
    public void loginOut() {
        clearToken();
        PreferencesUtils.saveString(PreContent.LOGIN_THREAD, "");
        UMenUtils.onProfileSignOff();
    }

    public void login() {
        new Navigator.Builder()
            .setPageName("login")
            .build().open();
    }

    public void sendLoginSuccess() {
        Intent intent = new Intent(LOGIN_SUCCESS);
        intent.addCategory(Intent.CATEGORY_DEFAULT);

        LocalBroadcastManager.getInstance(ContextUtils.getContext())
            .sendBroadcast(intent);
    }

    public void sendLoginOoutSuccess() {
        Intent intent = new Intent(LOGIN_OUT_SUCCESS);
        intent.addCategory(Intent.CATEGORY_DEFAULT);

        LocalBroadcastManager.getInstance(ContextUtils.getContext())
            .sendBroadcast(intent);
    }
}
